#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2003-2007 Jarek Zgoda <jarek.zgoda@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Main program startup file"""

__revision__ = '$Id: leech.py 7 2007-12-01 13:22:46Z jarek.zgoda $'

import os, sys
import signal

import pygtk
pygtk.require('2.0')
import gtk
import gobject

import const
import forms


class LeechApp(object):
    """Main application class"""

    def __init__(self):
        self.main_window = forms.MainWindow()
        self.main_window.show()


if __name__ == '__main__':
    if os.name == 'nt':
        pass
    elif os.name == 'posix':
        import fcntl
        pid_file = os.path.join(const.USER_DIR, 'leechfm.pid')
        fp = open(pid_file, 'w')
        try:
            fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            # another instance is running
            sys.exit(0)
    basedir = os.path.dirname(os.path.realpath(__file__))
    if basedir.endswith('/share/leech.fm'):
        sys.path.append(basedir[:-10] + 'lib/leech.fm')
    gobject.threads_init()
    import gettext, locale
    locale.setlocale(locale.LC_ALL, '')
    gtk.glade.bindtextdomain('leechfm', 'locale')
    gtk.glade.textdomain('leechfm')
    gettext.install('leechfm', 'locale', unicode=True)
    SIGNALS = [signal.SIGINT, signal.SIGTERM, signal.SIGHUP]
    for sig in SIGNALS:
        signal.signal(sig, gtk.main_quit)
    app = LeechApp()
    gtk.main()
    if os.path.isfile(pid_file):
        os.unlink(pid_file)
